package br.unisc.robokit.webcam;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

import br.unisc.robokit.connection.Conexao;


public class SWebCam extends JFrame implements ActionListener {
	private static final long serialVersionUID = 5440251390481144236L;
	private InfoComp IC;
	private Timer t = new Timer(100, this);
	private JLabel imgLbl;
	private BufferedImage[] leds = new BufferedImage[4];
	private BufferedImage[] mcs = new BufferedImage[2];
//	private BufferedImage mp;
	private BufferedImage none;
	private BufferedImage pilha;
	private double rotation;

	public SWebCam(InfoComp IC) {
		try {
			this.IC = IC;
			imgLbl = new JLabel("");

			getContentPane().add(imgLbl, BorderLayout.CENTER);
			for (int x = 0; x < 4; x++) {
				leds[x] = ImageIO.read(SWebCam.class
						.getResourceAsStream("/sresource/led" + (x + 1)
								+ ".jpg"));
			}
			for (int x = 0; x < 2; x++) {
				mcs[x] = ImageIO
						.read(SWebCam.class.getResourceAsStream("/sresource/mc"
								+ (x + 1) + ".jpg"));
			}
//			mp = ImageIO.read(SWebCam.class
//					.getResourceAsStream("/sresource/mp0.jpg"));
			pilha = ImageIO.read(SWebCam.class
					.getResourceAsStream("/sresource/pilha.jpg"));
			none = ImageIO.read(SWebCam.class
					.getResourceAsStream("/sresource/none.jpg"));
			t.start();

			imgLbl.setIcon(new ImageIcon(none));
			setResizable(false);
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		BufferedImage img = null;
//		Random r = new Random();
		if (IC.isLed1()) {
			img = leds[0];
		}
		if (IC.isLed2()) {
			img = leds[1];
		}
		if (IC.isLed3()) {
			img = leds[2];
		}
		if (IC.isLed4()) {
			img = leds[3];
		}
		if (IC.isMC1()) {
			img = mcs[0];
		}
		if (IC.isMC2()) {
			img = mcs[1];
		}
		if (IC.isMP()) {
			rotation += 10;
		}
		if (img == null) {
			img = none;
		}
		
		BufferedImage tmp = new BufferedImage(100, 100,
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = (Graphics2D) tmp.getGraphics();

		int sw2 = pilha.getWidth() / 2;
		int sh2 = pilha.getHeight() / 2;

		g.rotate(Math.PI * rotation / 360.0, sw2, sh2);
		g.drawImage(pilha, 0, 0, null);

		BufferedImage nImg = new BufferedImage(none.getWidth(), none.getHeight(),
				BufferedImage.TYPE_INT_RGB);
		g = (Graphics2D) nImg.getGraphics();
		g.drawImage(img, 0, 0, null);
		g.drawImage(tmp, 275, 45, null);
		
		imgLbl.setIcon(new ImageIcon(nImg));
		repaint();
	}

	public static void main(String[] args) throws UnknownHostException,
			IOException {
		InfoComp IC = new InfoComp();
		SWebCam sWebCam = new SWebCam(IC);
		sWebCam.setVisible(true);
		new Conexao(IC);
	}
}
